/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.common;

import java.util.ArrayList;
import java.util.List;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Cell;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Row;

/**
 *
 * @author sorasaks
 */
public abstract class DeployRadioRenderer extends DeployRowRenderer {

    private static final String EXECUTE_VALUE = "EXECUTE_VALUE";
    private List<Radio> listRadio = new ArrayList<Radio>();

    protected Cell createComponentInCell(Component cmpnt, Object obj, String cellStyle) throws Exception {
        Cell cell = new Cell();
        cell.setSclass(cellStyle);
        cell.appendChild(cmpnt);

        return cell;
    }

    protected Cell createRadioInCell(Object obj, String cellStyle, int diffNumber) throws Exception {
        Radio radio = new Radio();
        radio.setValue(diffNumber);
        radio.setAttribute(EXECUTE_VALUE, obj);

        listRadio.add(radio);

        radio.addEventListener(Events.ON_CHECK, new RadioEventListener(diffNumber));

        Cell cell = new Cell();
        cell.setSclass(cellStyle);
        cell.appendChild(radio);

        return cell;
    }

    private class RadioEventListener implements EventListener {

        private final int diffNumber;

        public RadioEventListener(int diffNumber) {
            this.diffNumber = diffNumber;
        }

        @Override
        public void onEvent(Event t) throws Exception {
            for (Radio radio : listRadio) {
                if (diffNumber == Integer.parseInt(radio.getValue().toString())) {
                    radio.setSelected(Boolean.FALSE);
                }
            }
            Radio radio = (Radio) t.getTarget();
            radio.setSelected(Boolean.TRUE);
        }
    }

    public class RowRadioEventListener implements EventListener {

        private final int diffNumber;

        public RowRadioEventListener(int diffNumber) {
            this.diffNumber = diffNumber;
        }

        @Override
        public void onEvent(Event event) throws Exception {
            for (Radio radio : listRadio) {
                if (diffNumber == Integer.parseInt(radio.getValue().toString())) {
                    radio.setSelected(Boolean.FALSE);
                }
            }
            Row row = (Row) event.getTarget();
            Cell cell = (Cell) row.getFirstChild();
            Radio radio = (Radio) cell.getLastChild();
            radio.setSelected(Boolean.TRUE);
        }
    }

    public List<Object> getObjectSelect() {

        List<Object> result = new ArrayList<Object>();
        
        for (Radio radio : listRadio) {
            if (radio.isSelected()) {
                Object object = radio.getAttribute(EXECUTE_VALUE);
                result.add(object);
            }
        }

        return result;
    }

}
